﻿using System;
using MessagePack.Formatters;

namespace MessagePack.Resolvers
{
	// Token: 0x02000105 RID: 261
	public class CompositeResolver : IFormatterResolver
	{
		// Token: 0x060003B5 RID: 949 RVA: 0x0000DCD1 File Offset: 0x0000C0D1
		private CompositeResolver()
		{
		}

		// Token: 0x060003B6 RID: 950 RVA: 0x0000DCD9 File Offset: 0x0000C0D9
		public static void Register(params IFormatterResolver[] resolvers)
		{
			if (CompositeResolver.isFreezed)
			{
				throw new InvalidOperationException("Register must call on startup(before use GetFormatter<T>).");
			}
			CompositeResolver.resolvers = resolvers;
		}

		// Token: 0x060003B7 RID: 951 RVA: 0x0000DCF6 File Offset: 0x0000C0F6
		public static void RegisterAndSetAsDefault(params IFormatterResolver[] resolvers)
		{
			CompositeResolver.Register(resolvers);
			MessagePackSerializer.SetDefaultResolver(CompositeResolver.Instance);
		}

		// Token: 0x060003B8 RID: 952 RVA: 0x0000DD08 File Offset: 0x0000C108
		public IMessagePackFormatter<T> GetFormatter<T>()
		{
			return CompositeResolver.FormatterCache<T>.formatter;
		}

		// Token: 0x04000159 RID: 345
		public static readonly CompositeResolver Instance = new CompositeResolver();

		// Token: 0x0400015A RID: 346
		private static bool isFreezed = false;

		// Token: 0x0400015B RID: 347
		private static IFormatterResolver[] resolvers = new IFormatterResolver[0];

		// Token: 0x02000106 RID: 262
		private static class FormatterCache<T>
		{
			// Token: 0x060003BA RID: 954 RVA: 0x0000DD2C File Offset: 0x0000C12C
			static FormatterCache()
			{
				CompositeResolver.isFreezed = true;
				foreach (IFormatterResolver formatterResolver in CompositeResolver.resolvers)
				{
					IMessagePackFormatter<T> messagePackFormatter = formatterResolver.GetFormatter<T>();
					if (messagePackFormatter != null)
					{
						CompositeResolver.FormatterCache<T>.formatter = messagePackFormatter;
						return;
					}
				}
			}

			// Token: 0x0400015C RID: 348
			public static readonly IMessagePackFormatter<T> formatter;
		}
	}
}
